

class viewshed3DAnalysis {
  constructor(viewer) {
    this.viewer = viewer
    this.viewshed3D = undefined
    this.point = undefined
    this.vsPointHandler = undefined
    this.clickFlag = 0;
    this.clickCount = 0;
    this.originViewshedObservationPlace = null;
  }

  remove() {
    var scene = this.viewer.scene;
    scene.viewFlag = true;
    if (this.vsPointHandler) {
      this.vsPointHandler && this.vsPointHandler.clear();
      this.vsPointHandler.deactivate();
    }
    if (this.viewshed3D) {
      this.viewshed3D.destroy();
      this.viewshed3D = undefined;
    }
  }

  analysis(formViewshed3D){
    let scene = this.viewer.scene
    this.clickFlag += 1
    this.clickCount = 0
    if (!this.viewshed3D) {
      this.viewshed3D = new Cesium.ViewShed3D(scene)
    }
    this.vsPointHandler && this.vsPointHandler.clear()
    this.viewshed3D.distance = 0.1
    this.viewshed3D.visibleAreaColor = Cesium.Color.fromCssColorString(formViewshed3D.visibleColor)
    this.viewshed3D.hiddenAreaColor = Cesium.Color.fromCssColorString(formViewshed3D.invisibleColor)

    let viewPosition;
    scene.viewFlag = true;
    this.vsPointHandler = new Cesium.DrawHandler(this.viewer, Cesium.DrawMode.Point, Cesium.ClampMode.Space);
    let vsHandler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
      vsHandler.setInputAction((e)=> {
        // 若此标记为false，则激活对可视域分析对象的操作
        if (!scene.viewFlag) {
          // 获取鼠标屏幕坐标,并将其转化成笛卡尔坐标
          let position = e.endPosition
          let last = scene.pickPosition(position)
          // 计算该点与视口位置点坐标的距离
          let distance = Cesium.Cartesian3.distance(viewPosition, last)
          if (distance > 0) {
            let cartographic = Cesium.Cartographic.fromCartesian(last)
            let longitude = Cesium.Math.toDegrees(cartographic.longitude)
            let latitude = Cesium.Math.toDegrees(cartographic.latitude)
            let height = cartographic.height
            this.viewshed3D.setDistDirByPoint([longitude, latitude, height])
          }
        }
      }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

      vsHandler.setInputAction((e)=> {
        if (!scene.viewFlag) {
          scene.viewFlag = true;
          formViewshed3D.directionAngle=this.viewshed3D.direction.toFixed(2)
          formViewshed3D.elevationAngle=this.viewshed3D.pitch.toFixed(2)
          formViewshed3D.visualRange=this.viewshed3D.distance.toFixed(2)
          formViewshed3D.horizontalFov=this.viewshed3D.horizontalFov.toFixed(2)
          formViewshed3D.verticalFov=this.viewshed3D.verticalFov.toFixed(2)
        }
        vsHandler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK);
      }, Cesium.ScreenSpaceEventType.RIGHT_CLICK)

    this.vsPointHandler.drawEvt.addEventListener((result)=> {
      this.point = result.object
      let position = this.point.position
      viewPosition = position
      let cartographic = Cesium.Cartographic.fromCartesian(position)
      let longitude = Cesium.Math.toDegrees(cartographic.longitude)
      let latitude = Cesium.Math.toDegrees(cartographic.latitude)
      let height = cartographic.height
      this.originViewshedObservationPlace = {longitude, latitude, height}
      let additionalHeight = Number(formViewshed3D.additionalHeight);
      let pointPosition = Cesium.Cartesian3.fromDegrees(longitude, latitude, height + additionalHeight);
      this.point.position = pointPosition;
      let viewshedObservationPlace = longitude.toFixed(4) + ', ' + latitude.toFixed(4) + ', ' + height.toFixed(2);
      formViewshed3D.observerInformation=viewshedObservationPlace
      if (scene.viewFlag) {
        this.viewshed3D.viewPosition = [longitude, latitude, height + additionalHeight];
        this.viewshed3D.build();
        scene.viewFlag = false;
      }
    });
    this.vsPointHandler.activate();
  }

  changeAdditionalHeight (value) {
    if (!this.originViewshedObservationPlace || !this.viewshed3D) {
      return;
    }
    var longitude = this.originViewshedObservationPlace.longitude;
    var latitude = this.originViewshedObservationPlace.latitude;
    if (value === "") { // 避免删除导致崩溃
      value = "0.0";
    }
    var height = this.originViewshedObservationPlace.height + parseFloat(value);
    var cartesian = Cesium.Cartesian3.fromDegrees(longitude, latitude, height);
    this.point.position = cartesian;
    this.viewshed3D.viewPosition = [longitude, latitude, height];
  }

  changeVisualRange(value) {
    if (Number(value) < 0.1) {
      value=0.1
    }
    this.viewshed3D.distance = Number(value)
  }

  changeElevationAngle(value) {
    if (value === "") {
      value=0
    }
    this.viewshed3D.pitch = parseFloat(value)
  }

  changeDirectionAngle(value){
    if (value === "") {
      value=0
    }
    this.viewshed3D.direction = parseFloat(value)
  }

  changeHorizontalFov(value){
    if (Number(value) < 1) {
      value=1;
    }
    this.viewshed3D.horizontalFov = parseFloat(value);
  }

  changeVerticalFov(value){
    if (Number(value) < 1) {
      value=1;
    }
    this.viewshed3D.verticalFov = parseFloat(value);
  }

  changeVisibleAreaColor(value){
    let color = Cesium.Color.fromCssColorString(value);
    this.viewshed3D.visibleAreaColor = color;
  }

  changeHiddenAreaColor(value){
    let color = Cesium.Color.fromCssColorString(value);
    this.viewshed3D.hiddenAreaColor = color;
  }
}
export {viewshed3DAnalysis}

